#ifndef RANDOM_CPP
#define RANDOM_CPP
#include "random.hpp"

MyRandom::MyRandom(const gsl_rng_type *t){
  r=gsl_rng_alloc(t);
  //gsl_rng_set(r,std::time(0));
}

MyRandom::MyRandom(const MyRandom& rng){
  if(this != &rng){
    r=gsl_rng_clone(rng.r);
  }
}

MyRandom& MyRandom::operator=(const MyRandom& rng){
  if(this != &rng ){
    r=gsl_rng_clone(rng.r);
  }
  return *this;
}

MyRandom::~MyRandom(){
  gsl_rng_free(r);
}

void MyRandom::set(){
  gsl_rng_set(r,std::time(0));
}

double MyRandom::sample(){
  return gsl_rng_uniform(r);
}

int MyRandom::sample_int_from(vector<double> p){
  double pC[p.size()];
  for(unsigned ii=0;ii<p.size();ii++){
    pC[ii]=p(ii);
  }
  gsl_ran_discrete_t* g=gsl_ran_discrete_preproc(p.size(),pC);
  int ret=gsl_ran_discrete(r,g);
  gsl_ran_discrete_free(g);
  
  return ret;
  
};

#endif
